package com.example.time;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author hej LocalDateTime 相减
 * @date 2022/12/28 18:42
 */
public class LocalTimeSub {
    public static void main(String[] args) {
        String start = "2010-01-02 01:01:01";
        String end = "2010-02-02 03:03:03";

        LocalDateTime startDateTime = LocalDateTime.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endDateTime = LocalDateTime.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));


        Duration duration = Duration.between(startDateTime, endDateTime);
        System.out.println(LocalTime.ofNanoOfDay(duration.toNanos()).format(DateTimeFormatter.ofPattern("HH:mm:ss")));
        BigDecimal remianTime = BigDecimal.valueOf(duration.getSeconds()).divide(new BigDecimal("3600"), 3, BigDecimal.ROUND_HALF_UP);
        System.out.println(remianTime);
    }
}
